package com.yex.modules.etcc.controller;

import com.yex.common.utils.PageUtils;
import com.yex.common.utils.R;
import com.yex.common.validator.ValidatorUtils;
import com.yex.common.validator.group.UpdateGroup;
import com.yex.entity.NoticeEntity;
import com.yex.entity.QuestionEntity;
import com.yex.entity.UserEntity;
import com.yex.service.NoticeService;
import com.yex.service.QuestionService;
import com.yex.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author Little Liu
 * @version V1
 * @date Created in 2018/12/27 16:50
 * @description 描述
 */
@RestController
@RequestMapping("/etcc")
public class
UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private NoticeService noticeService;


    /**
     * 所有用户列表
     */
    @RequestMapping("/user/list")
    public R list(@RequestParam Map<String, Object> params) {
        String mobile = (String) params.get("mobile");
        StringBuilder filter = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isBlank(mobile)) {
            filter.append(" and mobile='" + mobile + "'");
        }
        filter.append(" order by id asc");

        PageUtils page = userService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }

    @RequestMapping("/user/info/{id}")
    public R userInfo(@PathVariable("id") Long id) {
        UserEntity userEntity = userService.selectById(id);
        return R.ok().put("user", userEntity);
    }

    @RequestMapping("/user/saveLeader")
    public R saveLeader(@RequestBody UserEntity userEntity) {
        UserEntity user = userService.selectById(userEntity.getId());
        user.setLeader(1);
        user.setLeaderRate(userEntity.getLeaderRate());
        user.setLeaderTime(new Date());
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 所有用户列表
     */
    @RequestMapping("/identity/list")
    public R identityList(@RequestParam Map<String, Object> params) {
        String mobile = (String) params.get("mobile");

        StringBuilder filter = new StringBuilder(" where 1=1 and real_validate=1 and validate_status=0");
        if (mobile != null) {
            filter.append(" and mobile=" + mobile);
        }

        filter.append(" order by id asc");
        PageUtils page = userService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }


    @RequestMapping("/identity/pass")
    public R identityPass(@RequestBody Long[] userIds) {
        for (Long userId : userIds) {
            UserEntity userEntity = userService.selectById(userId);
            if (userEntity != null) {
                userService.updatePassIdentity(userEntity.getId());
            }
        }
        return R.ok();
    }


    @RequestMapping("/identity/noPass")
    public R identityNoPass(@RequestBody Long[] userIds) {
        for (Long userId : userIds) {
            UserEntity userEntity = userService.selectById(userId);
            if (userEntity != null) {
                userService.updateNoPassIdentity(userEntity.getId());
            }
        }
        return R.ok();
    }


    @RequestMapping("/question/list")
    public R questionList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 ");
        filter.append(" order by q.id desc");
        PageUtils page = questionService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }


    @RequestMapping("/question/info/{id}")
    public R info(@PathVariable("id") Long id) {
        QuestionEntity questionEntity = questionService.selectById(id);
        return R.ok().put("question", questionEntity);
    }

    @RequestMapping("/question/update")
    public R update(@RequestBody QuestionEntity questionEntity) {
        ValidatorUtils.validateEntity(questionEntity, UpdateGroup.class);

        if (StringUtils.isBlank(questionEntity.getReplyContent())) {
            return R.error("不能为空");
        }

        questionEntity.setReplyTime(new Date());
        questionService.updateById(questionEntity);
        return R.ok();
    }


    @RequestMapping("/notice/list")
    public R noticeList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 and status in (0,1) ");
        filter.append(" order by id desc");
        PageUtils page = noticeService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }

    @RequestMapping("/notice/save")
    public R noticeSave(@RequestBody NoticeEntity noticeEntity) {
        if (StringUtils.isBlank(noticeEntity.getContent()) || StringUtils.isBlank(noticeEntity.getTitle()) || noticeEntity.getType() == null) {
            return R.error("不能为空");
        }
        noticeEntity.setStatus(1);
        noticeEntity.setCreateTime(new Date());
        noticeEntity.setUpdateTime(new Date());
        noticeService.insert(noticeEntity);
        return R.ok();
    }

    @RequestMapping("/notice/delete")
    public R delete(@RequestBody Long[] ids) {
        for (Long id : ids) {
            NoticeEntity noticeEntity = noticeService.selectById(id);
            if (noticeEntity == null) {
                return R.error("不能为空");
            }
            noticeEntity.setUpdateTime(new Date());
            noticeEntity.setStatus(2);
            noticeService.updateById(noticeEntity);
        }
        return R.ok();
    }


}
